package com.dementors.ui.fragmentItem;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.dementors.R;
import com.dementors.base.BaseFragment;
import com.dementors.base.SpaceItemDecoration;
import com.dementors.data.API;
import com.dementors.model.SeaNew;
import com.dementors.net.http.okhttp.callback.OkHttpCallback;
import com.dementors.net.http.okhttp.request.OkHttpRequest;
import com.dementors.ui.activity.WebActivity;
import com.dementors.ui.adapter.SeaNewAdapter;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.util.ArrayList;


/**
 * Created by Administrator on 2015/12/2 0002.
 */
public class FragmentSeaItem extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView rv;
    private SwipeRefreshLayout srf;
    private ArrayList<String> urls = new ArrayList<>();
    private int type;
    private ArrayList<SeaNew.OverSeaBrands> brands;
    private SeaNewAdapter adapter;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };


    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_sea_new;
    }

    @Override
    protected void initViews() {
        rv = (RecyclerView) view.findViewById(R.id.rv_sea);
        srf = (SwipeRefreshLayout) view.findViewById(R.id.srflayout);
    }

    @Override
    protected void initData() {
        initUrls();//初始化Url,添加url地址
        initTypes();//初始化tyPe值，区分oversea中每个Fragment
        initRv();//初始化RecyclerView
        initNetData();//初始化网络数据
        initResigener();//设置监听
    }

    private void initResigener() {
        srf.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        srf.setOnRefreshListener(this);
        adapter.setOnItemClickListener(new SeaNewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, SeaNew.OverSeaBrands brandsbean) {

                    Intent intent=new Intent(getActivity(), WebActivity.class);
                    intent.putExtra("bid",brandsbean.bid);
                    startActivity(intent);

            }
        });
    }

    private void initRv() {
        brands = new ArrayList<>();
        adapter = new SeaNewAdapter(getActivity());
        adapter.setList(brands);
        rv.setAdapter(adapter);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        SpaceItemDecoration myDecoration = new SpaceItemDecoration(20);
        rv.addItemDecoration(myDecoration);

    }

    /**
     * 网络访问
     */
    private void initNetData() {
        new OkHttpRequest.Builder().url(urls.get(type)).get(new OkHttpCallback<SeaNew>() {

            @Override
            public void onError(Request request, Exception e) {
                Log.i("oye", "下载失败");
            }

            @Override
            public void onResponse(Response response, SeaNew data) {
                Log.i("oye", data.toString());
                brands.addAll(data.oversea_brands);
                adapter.setList(brands);

            }
        });
    }

    private void initTypes() {
        type = (int) getArguments().get("type");
    }

    private void initUrls() {
        urls.add(API.OVERSEA_NEW_DATA);
        urls.add(API.OVERSEA_DIAPER_DATA);
        urls.add(API.OVERSEA_MILK_DATA);
        urls.add(API.OVERSEA_FOOD_DATA);
        urls.add(API.OVERSEA_BABY_DATA);
        urls.add(API.OVERSEA_BEAUTY_DATA);

    }


    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {

                srf.setRefreshing(false);
            }
        }, 6000);
    }
}